package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.pojo.dto.PermissionAddNewDTO;
import cn.tedu.spotms.server.pojo.vo.PermissionStandardVO;
import cn.tedu.spotms.server.service.IPermissionService;
import cn.tedu.spotms.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permissions")
@Api(tags = "权限管理模块")
public class PermissionController {

    @Autowired
    private IPermissionService service;

    @PostMapping("/add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 10)
    @PreAuthorize("hasAuthority('/permission/update')")
    public JsonResult<Void> addNew(PermissionAddNewDTO permissionAddNewDTO) {
        service.addNew(permissionAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/list")
    @ApiOperation("权限列表")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/permission/read')")
    public JsonResult<List<PermissionStandardVO>> list() {
        List<PermissionStandardVO> list = service.list();
        return JsonResult.ok(list);
    }
}
